<?php

/*
**	FORM newPostForm
*/

$this->form_validation->set_rules('title', 'Title', 'trim|required');
$this->form_validation->set_rules('content', 'Content', 'trim|required');

if ($this->form_validation->run() == TRUE) {
	
	// Action 'Publish'
	if ($this->input->post('action:publish')) {

		$post = new Post();
		$post->setTitle($this->input->post('title'));
		$post->setContent($this->input->post('content'));
		if (!is_null($postId)) {
			$post->setPostId($postId);
			// update existing post
			$post = $this->Ligature_Post_model->updatePost($post);
		} else {
			// save new post
			$post = $this->Ligature_Post_model->createPost($post);
			// tweet about the post
			$this->Ligature_Post_model->tweetPost(
					$post->getPostId(),
					$this->session->userdata('current_user_screen_name'),
					$post->getTitle()
				);
		}

		// TODO: Compute the proper outcome
		$outcome = 'postPublished';

		switch ($outcome) {
			/*
				Outcome 'Post published'
			*/
			case 'postPublished':
				$messageClass = 'success';
				$this->session->set_flashdata('message', array('class' => $messageClass, 'text' => $this->lang->line('ligature_editPost_newPostForm_publish_postPublished_message')));
				redirect('/post/' . $post->getSlug());
				break;
			default:
				show_error("Unexpected outcome '$outcome' to action 'publish' in form 'newPostForm' on page 'editPost'.");
		}

	}
	else {
		show_error("Unexpected action '${action.id}' in form 'newPostForm' on page 'editPost'.");
	}

}

/* clickframes::version=464972501::clickframes */
?>
